package org.phalanx.servlet.websession;

import org.phalanx.messenging.Messageable;
import org.phalanx.server.ServerCache.CacheManager;
import org.phalanx.server.user.PhalanxUser;
import org.phalanx.server.user.UserManager;

public class UserWebSessionManager {
	private CacheManager cacheMgr;
	
	public UserWebSessionManager(CacheManager cacheMgr){
		this.cacheMgr = cacheMgr;
	}
	
	synchronized public void sendMessage(Integer uid, Messageable msg){
		PhalanxUser u = (PhalanxUser)cacheMgr.getElement("UserCache", uid);
		
		if (u != null){
			UserWebSession session = (UserWebSession)cacheMgr.getElement("UserWebFeedCache", u.getId());
			
			if (session != null){
				session.sendMessage(msg);
			}
		}
	}
	
	public void sendMessage(String username, Messageable msg){
		UserManager u = new UserManager(cacheMgr);
		
		PhalanxUser user = u.getUserByName(username);
		
		if (user != null){
			this.sendMessage(user.getId(), msg);
		}
	}
}
